//
//  XMJobListView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/21.
//

#import "XMJobListView.h"
#import "XMJobCell.h"

@interface XMJobListView ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UITextField *searchTextField;

@property (nonatomic, strong) NSDictionary *jobDic;

@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, assign) NSInteger itemSelectIndex;

@end

@implementation XMJobListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"选择职业";
    self.backgroundColor = kColorHEX(0xF0F0F0);
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - 44, kNavagationBarH - 44, 44, 44) title:@"确定" titleColor:kColorHEX(0x333333)];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:confirmBtn];
    
    _selectIndex = 0;
    _itemSelectIndex = -1;
//    [self addSubview:self.searchTextField];
    [self addSubview:self.tbl];
    [self addSubview:self.collectionView];
}

- (void)setContentWithJobList:(NSDictionary *)jobDic {
    _jobDic = jobDic;
    _selectIndex = 0;
    [_tbl reloadData];
    [_collectionView reloadData];
}

- (void)confirmAction:(UIButton *)button {
    if (_itemSelectIndex == -1) {
        [MBProgressHUD showError:@"请选择职业"];
        return;
    }
    NSString *key = [self.jobDic allKeys][_selectIndex];
    NSString *jobStr = [self.jobDic safeArrayForKey:key][_itemSelectIndex];
    self.jobSelectBlock ? self.jobSelectBlock(jobStr) : nil;
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_jobDic allKeys].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(46.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMJobCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = KFontSize(17.0);
    }
    cell.textLabel.text = [_jobDic allKeys][indexPath.row];//@"IT互联网";
    if (indexPath.row == _selectIndex) {
        cell.backgroundColor = kColorHEX(0xffffff);
        cell.textLabel.textColor = kColorHEX(0x333333);
    }
    else {
        cell.backgroundColor = kColorHEX(0xF0F0F0);
        cell.textLabel.textColor = kColorHEX(0x999999);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _selectIndex = indexPath.row;
    [tableView reloadData];
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    NSString *keyStr = [_jobDic allKeys][_selectIndex];
    return [_jobDic safeArrayForKey:keyStr].count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMJobCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMJobCellName" forIndexPath:indexPath];
    NSString *keyStr = [_jobDic allKeys][_selectIndex];
    [cell setContentWithTitle:[_jobDic safeArrayForKey:keyStr][indexPath.row] select:_itemSelectIndex == indexPath.row];
    XMWeakSelf;
    cell.itemSelectBlock = ^{
        weakSelf.itemSelectIndex = indexPath.row;
        [collectionView reloadData];
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init

- (UITextField *)searchTextField {
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), kNavagationBarH + KScaleWidth(10.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(44.0))];
        _searchTextField.font = KFontSize(15.0);
        _searchTextField.backgroundColor = kColorHEX(0xE1E1E1);
        _searchTextField.placeholder = @"输入职业名称查询";
        [_searchTextField setCornerRadius:_searchTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(30.0), _searchTextField.height)];
        UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(8.0), 0, KScaleWidth(14.0), _searchTextField.height)];
        searchIcon.contentMode = UIViewContentModeScaleAspectFit;
        searchIcon.image = ImageNamed(@"mine_search_icon");
        [leftView addSubview:searchIcon];
        _searchTextField.leftViewMode = UITextFieldViewModeAlways;
        _searchTextField.leftView = leftView;
    }
    return _searchTextField;
}

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH /*+ KScaleWidth(74.0)*/, KScaleWidth(112.0), self.height - kNavagationBarH /*- KScaleWidth(74.0)*/)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        CGFloat space = (self.width - _tbl.maxX - KScaleWidth(102.0)*2)/3;
        layout.sectionInset = UIEdgeInsetsMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(16.0));
        layout.minimumLineSpacing = KScaleWidth(16.0);
        layout.itemSize = CGSizeMake((self.width - _tbl.maxX - KScaleWidth(16.0)*3)/2, KScaleWidth(51.0));
//        layout.itemSize = CGSizeMake(KScaleWidth(102.0), KScaleWidth(51.0));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.tbl.maxX, _tbl.y, self.width - _tbl.maxX, _tbl.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[XMJobCell class] forCellWithReuseIdentifier:@"XMJobCellName"];
    }
    return _collectionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
